<?php
/**
 * Bdat Site development
 * @author Will Skates
 */

/*----------------------------------------------------------------------------------------------------*/

/**
 * Admin
 * Set's up the user interface for the administration system. 
 * Also determines how and what to use when listing/adding/editing and deleting.
 * @author Will Skates
 */

class admin extends CI_controller {
	
	//Arrays that relate to content types.
	public $requireCustomAdmin = array('User','Showcase','Setting','Tweet');
	public $ignore 			   = array('AggregatorBlogFeeds','UserProfiles','UserRoles','Gradyears','CiSessions');
	public $contentTypes 	   = array();
	public $currentType;
	public static $currentUser;
	public static $adminURL    = 'admin/';
	
	/**
	 * This function is loaded when admin/ is used.
	 * @return Void
	 */
	public function index()
	{
		$this->init();
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * A function to send the user to http://b.i-dat.org/.
	 * @return Void.
	 */
	public function getrid()
	{
		$this->load->Helper('url');
		header('Location: ' . base_url()); exit;
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Initialise all of the user interface elements.
	 * @return Void
	 */
	public function init()
	{
		
		//construct dashboard view
		//Load admin template with dashboard content.
		$this->load->Helper('url');
		
		self::$adminURL = base_url() . self::$adminURL;
		
		$this->load->View('admin/header',array('base'=>base_url()));
		
		//check that the user is logged in and has sufficient role to access the system.
		$this->load->Model('users/User');
		$this->User = $this->User->getLoggedInUser();
		
		if ($this->User === false || !$this->User->userHasAccess(2)) {
			$this->getrid();
		}
		
		//Make sure all classes can access the current user object by putting it.
		self::$currentUser = $this->User;
		
		//Load in the available content tables as different types.
		$this->load->Model('admin/adminModel');
		$this->contentTypes = $this->adminModel->getTypes($this);
		$this->load->helper('inflector');
		$this->load->Model('admin/ContentType');
		
		//Create admin menu with list links.
		$menu = array();
		foreach( $this->contentTypes as $key=>$val ) {
			
		if ( in_array($val,$this->requireCustomAdmin) ) {
				$menu[] = (Object)array('href'=>'/' . BDATURLBASE . '/admin/other/show/'. $val . '/', 'text'=>$val);
			} else {
				$menu[] = (Object)array('href'=>'/' . BDATURLBASE . '/admin/show/'. $val . '/', 'text'=>$val);
			}
			
		}
		
		//Show that menu to the user.
		$menu[] = (Object)array('href'=> base_url() . 'users/logout', 'text'=>'logOut');
		$this->load->View('admin/menu',array('links'=>$menu));
		
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Add a piece of content of a specific type.
	 * @param	String	$type - The type of content that we need to create.
	 * @return 	Void
	 */
	public function add( $type )
	{
		
		$this->init();
		$this->ContentType->init($type);
		$this->ContentType->add();
		//Get type form from content type.
		$this->denit();
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Edit a piece of content of a certain type.
	 * @param	String	$type - The type of content that we need to create.
	 * @param 	Integer $id	  - The unique identifier for the piece of content that we want to edit.
	 * @return	Void
	 */
	public function edit( $type, $id ) 
	{
		
		$this->init();
		$this->ContentType->init($type);
		$this->ContentType->edit($id);
		//Get form from content type and populate with data for that record. 
		$this->denit();
		
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Delete a piece of content of a certain type.
	 * @param	String	$type - The type of content that we need to create.
	 * @param 	Integer $id	  - The unique identifier for the piece of content that we want to edit.
	 * @return	Void
	 */
	public function delete($type, $id)
	{
		
		$this->init();
		$this->ContentType->init($type);
		$this->ContentType->delete($id);
		$this->denit();
		
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * List all of the content of a specific type.
	 * @param	String	$type - The type of content that we need to create.
	 * @return	Void
	 */
	
	public function show( $type )
	{
		
		$this->init();
		//List items with content type.
		$this->ContentType->init($type);
		$this->ContentType->show();
		$this->denit();
		
	}
		
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Perform an action for a piece of content which requires more than add/edit and delete functionality.
	 * @param $method
	 * @param $name
	 * @return unknown_type
	 */
	public function other( $method, $name )
	{
		
		//List the types of content which require additional functionality which have been implemented.
		$types = array('Showcase','showcase','tweet','Tweet','user','User');
		$this->init();
		
		//If the chosen content type has been implemented....
		if ( in_array($name,$types) ) {
			
			/*
			 * Get the additional arguments passed to this function in the url.
			 * E.g. http://b.i-dat.org/admin/other/show/user/promote/32/
			 * Will give us an array of (show, user, promote, 32);
			 * we will break down the array to this [promote,32].
			 */
			$args = func_get_args();
			unset($args[0],$args[1]);
			
			if (sizeof($args) > 0) {
				$args = array_chunk(array_filter($args),count($args)); $args = $args[0];
			} else {
				$args = array();
			}
			
			/*
			 * Load the model given the name that is passed to this function.
			 */
			$this->load->Model('admin/custom/' . ucfirst($name . 'admin'));
			$name = ucfirst($name . 'admin');
			
			//Launch it.
			$this->$name->launch($args);
			
		} else {
			
			//Otherwise tell the user that whatever name was given to the function hasn't been implemented.
			$this->load->View('admin/notice',array('notice'=>'custom cms implementations coming soon.'));
			
		}
		
		//Load in cms page model with specified name.
		$this->denit();
	}
	
	/*----------------------------------------------------------------------------------------------------*/
	
	/**
	 * Load the footer view.
	 * @return Void
	 */
	public function denit()
	{
		$this->load->View('admin/footer');
	}
	
	/*----------------------------------------------------------------------------------------------------*/

}